package com.greejoy.score.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.greejoy.score.R;

import pl.droidsonroids.gif.GifImageView;

public class WaitingActivity extends AppCompatActivity implements View.OnClickListener {


    private long logoutLastClickTime = 0;

    private TextView tv_judge_name;
    private GifImageView giv_ws_loading;

    private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(getString(R.string.broadcast_action))){
                String msg = intent.getStringExtra("msg");
                switch (msg){
                    case "showWsLoading":
                        giv_ws_loading.setVisibility(View.VISIBLE);
                        break;
                    case "hideWsLoading":
                        giv_ws_loading.setVisibility(View.INVISIBLE);
                        break;
                }
            }
        }
    };

    @Override
    public void onBackPressed() {
        // 屏蔽返回
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_waiting);

        Bundle bundle = getIntent().getExtras();
        String judgeName = bundle.getString("judge_name");
        tv_judge_name = findViewById(R.id.tv_judge_name);
        tv_judge_name.setText(judgeName);
        tv_judge_name.setOnClickListener(this);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(getString(R.string.broadcast_action));
        registerReceiver(myBroadcastReceiver, intentFilter);

        giv_ws_loading = findViewById(R.id.giv_ws_loading);

        Log.d("zhuwei", "WaitingActivity onCreate");

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        Bundle bundle = intent.getExtras();
        String judgeName = bundle.getString("judge_name");
        tv_judge_name.setText(judgeName);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.tv_judge_name){
            if (System.currentTimeMillis() - logoutLastClickTime < 500) {
                Intent intent = new Intent(WaitingActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            logoutLastClickTime = System.currentTimeMillis();
        }
    }

}